
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<div class="modal" id="addLineDiv" tabindex="-1" role="dialog" aria-hidden="true">

	<div class="modal-dialog" role="document">

		<div class="modal-content animated bounceInRight">

			<div class="modal-header">
				<h4 class="modal-title">字段行编辑</h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>

			<div class="modal-body" style="padding-bottom: 20px;">
				<%-- 导入提示信息框 --%>
			    <c:if test="${hint!=null&&hint!=''}">
			   		<jsp:include page="../../common/alert/alert.jsp">
			   			<jsp:param value="${hint}" name="alertType"/>
			   			<jsp:param value="${alertMessage}" name="alertMessage"/>
			   		</jsp:include>
			    </c:if>
			
				<form id="cform">
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>字段名称</label>
						<div class="col-sm-9">
							<select class="select2 form-control" name="fieldName" id="fieldName">
	                        	<option value="" selected="selected">请选择...</option>
	                        	<c:forEach var="field" begin="1" end="10" step="1">
	                        		<option value="attribute${field}">attribute${field}</option>
	                        	</c:forEach>
	                        </select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>字段描述</label>
						<div class="col-sm-9">
							<input id="fieldDesc" name="fieldDesc" type="text" class="form-control" value="${requestScope.cuxFlexFieldLine.fieldDesc}">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>字段类型</label>
						<div class="col-sm-9">
							<select class="select2 form-control" name="fieldAttributeType" id="fieldAttributeType">
	                        	<option value="" selected="selected">请选择...</option>
	                        	<c:forEach items="${requestScope.fieldAttributeTypeMap}" var="fieldAttributeType">
	                        		<option value="${fieldAttributeType.key}">${fieldAttributeType.value}</option>
	                        	</c:forEach>
	                        </select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label">字段值集</label>
						<div class="col-sm-9">
							<select class="chosen-select form-control" name="fieldAttributeDataset" id="fieldAttributeDataset">
	                        	<option value="" selected="selected">请选择...</option>
	                        	<c:forEach items="${requestScope.sysDatasetTypeList}" var="sysDatasetType">
	                        		<option value="${sysDatasetType.datasetTypeCode}">${sysDatasetType.datasetTypeName}</option>
	                        	</c:forEach>
	                        </select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>

					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>状态</label>
						<div class="col-sm-9">
							<select class="form-control m-b" name="status" id="status">
								<option value="Y" selected="selected">有效</option>
								<option value="N">无效</option>
							</select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>

					<div class="form-group row m-b-none">
						<div class="col-sm-12 col-sm-offset-2 text-right">
							<button class="btn btn-white btn-lg" type="button"
								data-dismiss="modal">返回</button>
							&nbsp;
							<button
								class="ladda-button ladda-button-demo btn btn-primary btn-lg"
								data-style="expand-right">
								&nbsp;&nbsp;确定&nbsp;&nbsp;<i class="fa fa-check-square-o"></i>
							</button>
						</div>
					</div>
					
					<input type="hidden" id="ffHeadCode" name="ffHeadCode" value="${param.ffHeadCode}"> 
					<input type="hidden" id="ffLineId" name="ffLineId" value="${requestScope.cuxFlexFieldLine.ffLineId}"> 
					<input type="hidden" id="createdDate" name="createdDate" value="${requestScope.cuxFlexFieldLine.createdDate}"> 
					<input type="hidden" id="createdBy" name="createdBy" value="${requestScope.cuxFlexFieldLine.createdBy}">
				</form>
			</div>

		</div>

	</div>

</div>

<!-- Chosen -->
<script src="js/plugins/chosen/chosen.jquery.js"></script>

<script>
	$(document).ready(function() {
		//初始化status
		if("${requestScope.cuxFlexFieldLine.status}"!=""){
			$("#status").val("${requestScope.cuxFlexFieldLine.status}");
		}
		//初始化fieldName
		if("${requestScope.cuxFlexFieldLine.fieldName}"!=""){
			$("#fieldName").val("${requestScope.cuxFlexFieldLine.fieldName}");
		}
		//初始化fieldAttributeType
		if("${requestScope.cuxFlexFieldLine.fieldAttributeType}"!=""){
			$("#fieldAttributeType").val("${requestScope.cuxFlexFieldLine.fieldAttributeType}");
		}
		//初始化fieldAttributeDataset
		if("${requestScope.cuxFlexFieldLine.fieldAttributeDataset}"!=""){
			$("#fieldAttributeDataset").val("${requestScope.cuxFlexFieldLine.fieldAttributeDataset}");
		}
		
		//初始化chosen-select
		$('.chosen-select').chosen({width: "100%"});
		
		//切换字段类型效果
		$("#fieldAttributeType").change(function(){
			if($("#fieldAttributeType").val()!="DATASET"){
				$("#fieldAttributeDataset").val("");
				$("#fieldAttributeDataset").trigger("chosen:updated");
			}
		});
		
		//去除已存在的辅助字段
		<%if(request.getParameter("ffLineId")==null){%>
		$("#fieldName option").each(function(){
			var option = $(this);
			$("td.fieldName").each(function(){
				if($(this).text()==$(option).val()){
					$(option).remove();
					return false;
				}
			});
		});
		<%}%>
		
		
		
		//表单提交
		var l = $('.ladda-button-demo').ladda();

		l.click(function() {
			$("#cform").valid();
			//l.ladda('stop');
		});

		$("#cform").validate({
			rules : {
				fieldName : {
					required : true,
				},
				fieldDesc : {
					required : true,
				},
				fieldAttributeType : {
					required : true,
				},
				status : {
					required : true,
				}
			},
			submitHandler: function(form) {
				var submitFlag = "Y";
				
				if($("#fieldAttributeType").val()=="DATASET"&&$("#fieldAttributeDataset").val()==""){
					submitFlag = "N";
					redragonJS.alert("请选择字段值集");
				}else if($("#fieldAttributeType").val()!="DATASET"&&$("#fieldAttributeDataset").val()!=""){
					submitFlag = "N";
					redragonJS.alert("非值集字段不能选择值集");
				}
				
				if(submitFlag=="Y"){
					l.ladda('start');
					editLine();
				}
		    }
		});
		
	});
	
	//异步编辑行
	function editLine(){
		redragonJS.loading("tabDiv");
	
		$.ajax({
			type: "post",
			url: "web/cuxFlexFieldLine/editCuxFlexFieldLine",
			data: {"fieldName": $("#fieldName").val(), "fieldDesc": $("#fieldDesc").val(), "fieldAttributeType": $("#fieldAttributeType").val(), "fieldAttributeDataset": $("#fieldAttributeDataset").val(),
				   "status": $("#status").val(), "ffLineId": $("#ffLineId").val(), "createdDate": $("#createdDate").val(), "createdBy": $("#createdBy").val(), 
				   "ffHeadCode": $("#ffHeadCode").val()},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				redragonJS.removeLoading("tabDiv");
				$('#addLineDiv').modal('hide');
				$('.ladda-button-demo').ladda('stop');
				
				var json = JSON.parse(data);
				if(json.result=="success"){
					getLineTab($("#ffHeadCode").val());
				}else{
					redragonJS.alert("编辑字段行错误");
				}
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}

</script>
